package unv.esiag.uchiha.readers;

import java.io.Serializable;
import java.util.Vector;

import unv.esiag.uchiha.operations.Operation;
import unv.esiag.uchiha.semaphore.Semaphore;
import unv.esiag.uchiha.subscribers.impl.SubscriberImpl;

public class DataReactor {
	private Vector<DataListener> listeners;
	private Semaphore listenerS;
	private SubscriberImpl subscriber;
	public DataReactor() {
		this.listeners = new Vector<DataListener>();
		this.listenerS = new Semaphore();
		this.subscriber = SubscriberImpl.getSubscriber(this);
	}
	public SubscriberImpl getSubscriber() {
		return subscriber;
	}
	public void addDataListener(DataListener listener){
		this.listenerS.stop();
		this.listeners.add(listener);
		this.listenerS.signal();
	}
	public void addOperation(Serializable serializable){
		Operation operation = (Operation) serializable;
		this.listenerS.stop();
		for(int i = 0;i<this.listeners.size();i++){
			if(operation.getData().getTopicName().equals(this.listeners.get(i).getTopicName())){
				this.listeners.get(i).signal(operation);
			}
		}
		this.listenerS.signal();
	}
}
